package com.huang.mvvm.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.databinding.ViewDataBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.huang.mvvm.control.ControlProvider


import com.huang.mvvm.vm.base.BaseFragmentViewModel
import org.greenrobot.eventbus.EventBus

/**
 * Created by laohuang on 2018/9/9.
 */

abstract class BaseFragment<V : ViewDataBinding, VM : BaseFragmentViewModel<*>> :
    BaseMvvMFragment<V, VM>() {

    private val postRefreshEvent = LinkedHashMap<Int, Any?>(10)

    protected lateinit var controllerProvider: ControlProvider

    protected var isFirstVisible = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        controllerProvider = ControlProvider(this)
        initParams()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = super.onCreateView(inflater, container, savedInstanceState)
        return attachToSwipeBack(view!!)
    }

    override fun onCreatedViewModel() {
        super.onCreatedViewModel()
        initDaggerInject()
    }

    override fun onBeforeObservable() {
        super.onBeforeObservable()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewModelParams()
        initView()
        viewModel.initEvent()
    }

    override fun onStart() {
        super.onStart()
        if (isRegisterEventBus()) EventBus.getDefault().register(viewModel)
    }

    override fun onResume() {
        super.onResume()
        getImmersionBar()?.init()
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(viewModel)
    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        getImmersionBar()?.init()
        if (!isFirstVisible)
            for (event in postRefreshEvent) onRefresh(event.key, event.value)
        postRefreshEvent.clear()
    }

    override fun onSupportInvisible() {
        super.onSupportInvisible()
    }

    override fun onEnterAnimationEnd(savedInstanceState: Bundle?) {
        super.onEnterAnimationEnd(savedInstanceState)
        if (isInitDataAfterAnimation() && isFirstVisible) {
            viewModel.initData()
            isFirstVisible = false
        }
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        if (!isInitDataAfterAnimation()) {
            viewModel.initData()
            isFirstVisible = false
        }
    }


    override fun onDestroyController() {
        controllerProvider.get().onDestroy()
    }

    /**
     * 在fragment有空(出现在用户面前)的时候再刷新
     */
    fun postRefresh(type: Int, data: Any? = null) {
        if (isSupportVisible) onRefresh(type, data)
        else postRefreshEvent[type] = data
    }

    /**
     * 立即刷新 ，推荐使用PostRefresh
     */
    open fun onRefresh(type: Int, data: Any?) {}


    override fun showLoading(
        isCancelEnable: Boolean,
        isBackEnable: Boolean,
        onCanceledListener: (() -> Unit)?
    ) {
        controllerProvider.get().showLoading(this,isCancelEnable, isBackEnable, onCanceledListener)
    }

    override fun hideLoading() {
        controllerProvider.get().hideLoading()
    }

    override fun showMessage(text: String) {
        controllerProvider.get().showMessage(text)
    }

    /** 是否注册EventBus */
    open fun isRegisterEventBus(): Boolean {
        return false
    }

    /** 设置状态栏效果  */
    open fun getImmersionBar(): ImmersionBar? {
        return ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
            .navigationBarDarkIcon(true)
            .navigationBarColor(android.R.color.white)
    }

    /** 是否再加载完动画后才开始加载数据，这样是为了防止卡顿 ，默认：加载动画的同时调用 initData() */
    open fun isInitDataAfterAnimation(): Boolean {
        return false
    }

    /** 初始化页面参数  */
    open fun initParams() {

    }

    /** Dagger注入 */
    open fun initDaggerInject(){

    }

    /** 初始化或传递ViewModel的参数  */
    open fun initViewModelParams() {

    }

    /** 初始化视图  */
    open fun initView() {

    }


}
